﻿using System;
using System.ServiceModel;
using AutoMapper;
using DataTransferObject;
using DomainModel;
using EntityFrameworkRepository;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace RemoteFacade
{
    class Program
    {
        static void Main()
        {
            var program = new Program();

            Console.Title = "TechDays 2012 - WCF Service";
            
            Console.WriteLine("-------------------------------------------");
            Console.WriteLine("               WCF Service ");
            Console.WriteLine("-------------------------------------------");

            Console.WriteLine("> Registering types ... ");
            program.InitializeServiceLocator();
            program.RegisterTypes();
            program.CreateDTOMappings();

            Console.WriteLine("> Service starting ... ");

            var host = new ServiceHost(typeof (SignalementManagementRemoteService));
            host.Open();
            
            Console.WriteLine("> Service started ...");
            Console.ReadLine();

            host.Close();
        }

        private void InitializeServiceLocator()
        {
            var unityServiceLocator = new UnityServiceLocator(new UnityContainer());
            ServiceLocator.SetLocatorProvider(() => unityServiceLocator);
        }

        private void RegisterTypes()
        {
            var container = ServiceLocator.Current.GetInstance<IUnityContainer>();

            // Repositories
            container.RegisterType<ISignalementRepository, SignalementRepository>();
            container.RegisterType<IObjetPatrimonialRepository, ObjetPatrimonialRepository>();
            container.RegisterType<IPanneRepository, PanneRepository>();
        }

        private void CreateDTOMappings()
        {
            Mapper.CreateMap<ObjetPatrimonial, ObjetPatrimonialDTO>();
            Mapper.CreateMap<ObjetPatrimonialDTO, ObjetPatrimonial>();
            Mapper.CreateMap<Panne, PanneDTO>();
            Mapper.CreateMap<PanneDTO, Panne>();
            Mapper.CreateMap<Signalement, SignalementDTO>();
            Mapper.CreateMap<SignalementDTO, Signalement>();
        }
    }
}
